<template>
  <div>
    <el-breadcrumb separator="/" style="margin-bottom: 20px;">
      <el-breadcrumb-item :to="{ path: '/' }">套餐管理</el-breadcrumb-item>
      <el-breadcrumb-item>套餐列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索 -->
      <el-form ref="CuisineFormRef" :model="CuisineForm" label-width="120px">
        <el-row>
          <el-col :lg="8" :md="12" :sm="12" :xs="24">
            <el-form-item label="菜品类型">
              <el-input
                v-model="CuisineForm.models"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5" style="text-align: right;">
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>
          <el-col :span="11" style="text-align: right;">
            <el-button @click="addDialog" type="warning">添加菜品</el-button>
          </el-col>
        </el-row>
      </el-form>

      <!-- 表格 -->
      <el-table
        :data="CuisineData"
        border
        :header-cell-style="{ 'text-align': 'center' }"
        style="width: 100%;"
      >
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          v-for="(item, index) in tableColumn"
          :key="index"
          :prop="item.prop ? item.prop : undefined"
          :label="item.label"
          align="center"
          :width="item.width"
        >
          <template slot-scope="scope">
            <div v-if="item.type === 'role'">
              {{ scope.row[item.prop] | role }}
            </div>
            <img
              v-if="item.type === 'img'"
              :src="scope.row[item.prop]"
              alt=""
              style="width: 150px; height: 120px;"
            />
            <div v-else-if="item.type === 'date'">
              {{ scope.row[item.prop] | timeFilter }}
            </div>
            <div v-else>{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="editDialog(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="delClick(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row style="text-align: right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="菜品类型" prop="models">
          <el-select
            v-model="addForm.models"
            placeholder="请选择菜品类型"
            style="width: 100%;"
          >
            <el-option
              v-for="(item, index) in CuisineModel"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产地址" prop="FoodAddress">
          <el-input v-model="addForm.FoodAddress"></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="date">
        <el-date-picker
         v-model="addForm.date"
         align="right"
         type="datetime"
         placeholder="选择日期">
</el-date-picker>
        </el-form-item>
        <el-form-item label="价格" prop="CuisinePrice">
          <el-input v-model="addForm.CuisinePrice"></el-input>
        </el-form-item>
        <el-form-item label="菜品图片" prop="CuisineImg">
          <el-upload
            :action="`${BASEURL}:5000/api/Cuisine/upload`"
            list-type="picture-card"
            :limit="1"
            :multiple="false"
            :on-change="uploadImage"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :file-list="fileList"
          >
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetClick">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
const tableColumn = [
  { label: "菜品类型", prop: "models", type: "text" },
  { label: "生产地址", prop: "FoodAddress", type: "text" },
  { label: "生产日期", prop: "date", type: "date" },
  { label: "图片", prop: "CuisineImg", type: "img", width: 200 },
  { label: "价格", prop: "CuisinePrice", type: "text" }
];
export default {
  data() {
    return {
      CuisineForm: {},
      CuisineData: [],
      addForm: {},
      fileList: [],
      BASEURL: "http://localhost",
      rules: {
        models: [
          { required: true, message: "请选择菜品类型", trigger: "change" }
        ],
        FoodAddress: [{ required: true, message: "请输入菜品生产地址", trigger: "blur" }],
        date: [{ required: true, message: "请输入生产日期", trigger: "blur" }],
        CuisinePrice: [{ required: true, message: "请输入价格", trigger: "blur" }]
      },
      currentPage: 5,
      total: 0,
      pageSize: 5,
      pageNum: 1,
      tableColumn,
      dialogVisible: false
    };
  },
  mounted() {
    this.getList();
  },
  computed: {
    ...mapState(["CuisineModel"])
  },
  methods: {
    getList() {
      this.CuisineForm.pageSize = this.pageSize;
      this.CuisineForm.pageNum = this.pageNum;
      this.$api
        .get("/api/Cuisine/list", { params: this.CuisineForm })
        .then(res => {
          this.CuisineData = res.data.data.records;
          this.total = res.data.data.total;
        })
        .catch(err => {
          console.log(err);
          this.$message.error("获取列表失败");
        });
    },
    search() {
      this.pageNum = 1;
      this.getList();
    },
    reset() {
      this.pageNum = 1;
      this.CuisineForm = {};
      this.getList();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.pageNum = 1;
      this.getList();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.getList();
    },
    addDialog() {
      this.addForm = {};
      this.dialogVisible = true;
    },
    editDialog(row) {
      this.addForm = row;
      this.dialogVisible = true;
    },
    handleClose() {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.resetClick();
        })
        .catch(_ => {});
    },
    addClick() {
      if (this.addForm._id) {
        this.$api
          .post(`/api/Cuisine/edit/${this.addForm._id}`, this.addForm)
          .then(res => {
            this.addForm = {};
            this.dialogVisible = false;
            this.getList();
           this.$message({
                type: "success",
                message: "编辑成功!"
              });
          });
      } else {
        this.$api.post(`/api/Cuisine/add`, this.addForm).then(res => {
          this.addForm = {};
          this.dialogVisible = false;
          this.getList();
          this.$message({
                type: "success",
                message: "添加成功!"
              });
        });
      }
    },
    resetClick() {
      this.$refs.addFormRef.resetFields();
      this.dialogVisible = false;
    },
    delClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api
            .delete(`/api/Cuisine/delete/${row._id}`)
            .then(res => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.pageNum = 1;
              this.getList();
            })
            .catch(err => {
              this.$message({
                type: "error",
                message: "删除失败"
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 上传
    handleRemove(file, fileList) {},
    handlePictureCardPreview(file) {
        // this.dialogImageUrl = file.url;
        // this.dialogVisible = true;
    },
    uploadImage(file, fileList) {
      console.log(file);
      if (file.response) {
        this.addForm.CuisineImg = file.response[0].path;
      }
    }
  }
};
</script>

<style></style>
